import json

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse, FileResponse
from .models import Data
from .models import City
import re
from urllib import parse

globals_city = ''


def add(request):
    global globals_city
    city = globals_city
    if request.method == 'POST':

        # place = request.POST.get('place', None)
        classname = request.POST.get('classname', None)
        title = request.POST.get('title', None)
        cell = request.POST.get('cell', None)
        source_price = request.POST.get('source_price', None)
        price = request.POST.get('price', None)
        sku = request.POST.get('sku', None)

        Data.objects.create(place=city, classname=classname, title=title,
                            cell=cell, source_price=source_price,
                            price=price, sku=sku, )

        return HttpResponse('{"code":200, msg:"OK"}')
    else:
        place = request.GET.get('place', None)
        classname = request.GET.get('place', None)
        title = request.GET.get('place', None)
        cell = request.GET.get('place', None)
        source_price = request.GET.get('place', None)
        price = request.GET.get('place', None)
        sku = request.GET.get('place', None)

        Data.objects.create(place=place, classname=classname, title=title,
                            cell=cell, source_price=source_price,
                            price=price, sku=sku, )

        return HttpResponse('{"code":200, msg:"OK"}')


def update_city(request):
    global globals_city
    if request.method == 'POST':
        city = request.POST.get('city', None)
        globals_city = city
        print(city)
        City.objects.create(city=city)
        return HttpResponse('{"code":200,"msg":"Update the ' + city + ' city is OK"}')

    else:
        city = request.GET.get('city', None)
        City.objects.create(city=city)
        globals_city = city
        print(city)
        return HttpResponse('{"code":200,"msg":"Update the ' + city + ' city is OK"}')


def get_now_city(request):
    global globals_city

    return HttpResponse('{"code":200,"msg":"' + globals_city + '"}')


def parser_json_hema(request):
    global globals_city
    city = globals_city
    if request.method == 'POST':
        json_data = json.loads(request.body.decode("utf-8"))
        # respon_body = request.POST.get('response_data', None)

        respon_body = json_data['response_data']
        # print(request.body)

        # url = request.POST.get('url', None)
        url = json_data['url']

        if respon_body:

            classname = re.findall('[\u4e00-\u9fa5]{4}', parse.unquote(url))[0]
            datalist = respon_body['data']['dataList']

            for data in datalist:
                item = data['item']
                title = item['title']  # 标题
                SellCount = item['recentSellCount']  # 月销
                sourceprice = int(item['price']) / 100.0  # 原价
                price = int(item['promotion']['promotionPrice']) / 100.0  # 现价
                # price = 0
                realInventory = item['realInventory']  # 真实库存
                csv_data = [classname, title, SellCount, sourceprice, price, realInventory]
                Data.objects.create(place=city, classname=classname, title=title,
                                    cell=SellCount, source_price=sourceprice,
                                    price=price, sku=realInventory, )
                print(csv_data)

    return HttpResponse("OK")
